package com.neu.fenli2023623.security;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginFilter extends AbstractAuthenticationProcessingFilter {

    //定义过滤器拦截的请求映射
    private final static String matchUrl = "/users/login";

    public LoginFilter() {
        super(new AntPathRequestMatcher(matchUrl));
    }
    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
        String userAccount = request.getParameter("account");
        String password = request.getParameter("pwd");
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userAccount, password);
//      //开启认证
        return this.getAuthenticationManager().authenticate(authenticationToken);
    }
}